package cn.cds.withyou.admin.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.cds.withyou.admin.R;
import cn.cds.withyou.lib.model.VIPRequestDto;

/**
 * <p> Description: </p>
 * <p/>
 * <p> Copyright: Copyright (c) 2016 </p>
 *
 * @author: Json.Lee
 * @Version: 1.0
 * @CreateTime: 7/5/2016 20:52
 */
public class VIPRequestAdapter extends RecyclerView.Adapter<VIPRequestAdapter.VIPRequestHolder> {

    private Context mContext;
    private List<VIPRequestDto> mData;
    private VIPRequestHandleListener mListener;

    public VIPRequestAdapter(Context context, @NonNull List<VIPRequestDto> data, @NonNull VIPRequestHandleListener listener) {
        this.mContext = context;
        this.mData = data;
        this.mListener = listener;
    }

    @Override
    public VIPRequestHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.activity_vip_request_item, parent, false);
        return new VIPRequestHolder(v);
    }

    @Override
    public void onBindViewHolder(VIPRequestHolder holder, int position) {
        final VIPRequestDto dto = mData.get(position);
        holder.nameTv.setText(dto.getName());
        holder.genderTv.setText(dto.getGender());
        holder.phoneTv.setText(dto.getPhone());
        holder.qqTv.setText(dto.getQq());
        holder.collegeTv.setText(dto.getCollege());
        holder.professionTv.setText(dto.getProfession());
        if (TextUtils.isEmpty(dto.getPassed())) {
            holder.handleLl.setVisibility(View.VISIBLE);
            holder.resultLl.setVisibility(View.GONE);
            holder.acceptBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.acceptRequest(dto);
                }
            });
            holder.rejectBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.rejectRequest(dto);
                }
            });
        } else {
            holder.handleLl.setVisibility(View.GONE);
            holder.resultLl.setVisibility(View.VISIBLE);
            if (VIPRequestDto.ACCEPT.equalsIgnoreCase(dto.getPassed())) {
                holder.resultTv.setText("已同意");
            } else {
                holder.resultTv.setText("已拒绝");
            }
        }
    }

    @Override
    public int getItemCount() {
        return null == mData ? 0 : mData.size();
    }

    public static class VIPRequestHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.name_tv)
        TextView nameTv;
        @Bind(R.id.gender_tv)
        TextView genderTv;
        @Bind(R.id.phone_tv)
        TextView phoneTv;
        @Bind(R.id.qq_tv)
        TextView qqTv;
        @Bind(R.id.college_tv)
        TextView collegeTv;
        @Bind(R.id.profession_tv)
        TextView professionTv;
        @Bind(R.id.accept_btn)
        Button acceptBtn;
        @Bind(R.id.reject_btn)
        Button rejectBtn;
        @Bind(R.id.handle_ll)
        LinearLayout handleLl;
        @Bind(R.id.result_tv)
        TextView resultTv;
        @Bind(R.id.result_ll)
        LinearLayout resultLl;

        public VIPRequestHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface VIPRequestHandleListener {
        void acceptRequest(VIPRequestDto dto);

        void rejectRequest(VIPRequestDto dto);
    }
}
